﻿using MediatR;
using Process.Api.Application.Command.BOM;
using Process.BLL;
using Process.DAL;
using Process.ErrorCod;
using Process.Model;

namespace Process.Api.Application.Handler.BOM
{
    public class DeleteGoodsinfoHandler : IRequestHandler<DeleteGoodsinfoCommand, ApiResult<int>>
    {
        private readonly IProcessRepository<Goodsinfo> goodsinfoRepo;

        public DeleteGoodsinfoHandler(IProcessRepository<Goodsinfo> goodsinfoRepo)
        {
            this.goodsinfoRepo = goodsinfoRepo;
        }
        /// <summary>
        /// 删除物品数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(DeleteGoodsinfoCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>();

            var query = goodsinfoRepo.GetAllAsync().FirstOrDefault(x => x.Id == request.Id && !x.IsDelete);
            if (query != null)
            {
                query.IsDelete = true;
            }
            var res = goodsinfoRepo.UpdateAsync(query).Result;
            if (res > 0)
            {
                result.Code = ErrorStatus.Success;
                result.Message = "删除物品数据成功";
                result.Data = res;
            }
            else
            {
               
                result.Code = ErrorStatus.Error;
                result.Message = "删除物品数据失败";
                result.Data = res;
            }
            return result;
        }
    }
}
